﻿using System;
using System.Windows.Forms;
using Symbol.Barcode;

namespace Saturn.App.Common
{
    public class GetScanData
    {

        public static Action<string> CallScan;

        #region ***   开启扫描   ***
        /// <summary>
        /// 
        /// </summary>
        public static void OpenScan()
        {
            if (!ReaderAction.InitReader())
            {
                MessageBox.Show("扫描引擎启动失败!");
            }

            //同时再次注册扫描事件
            ReaderAction.reader.ReadNotify += MyReader_ReadNotify;
            ReaderAction.StartRead();
        }
        #endregion


        #region ****----  扫描事件注册的方法  ----****
        /// <summary>
        /// 扫描事件注册的方法
        /// </summary>
        public static void MyReader_ReadNotify(object o, EventArgs e)
        {
            ReaderData readerdata = ReaderAction.reader.GetNextReaderData();
            if (readerdata.Result == Symbol.Results.SUCCESS)
            {
                ReaderAction.StartRead();//这里需要再次打开条码扫描模块
                string strAction = readerdata.Text;//扫描条码
                if (!string.IsNullOrEmpty(strAction))
                {
                    CallScan.Invoke(strAction);
                }

            }
        }
        #endregion


        #region ***   移除扫描   ***
        /// <summary>
        /// 
        /// </summary>
        public static void RemoveScan()
        {
            //移除扫描事件
            ReaderAction.StopRead();
            ReaderAction.reader.ReadNotify -= MyReader_ReadNotify;
        }
        #endregion


        

    }
}